package com.guava.string;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Map;

/**
 * SpiltterTester--GuavaTest
 *
 * @author: Jovi
 * @createTime: 2018-05-11 14:14
 **/
public class SpiltterTester {

    public static void main(String[] args) {

        Splitter splitter = Splitter.on('/');
        splitter.trimResults(); // does nothing!
        System.out.println(splitter.split("wrong / wrong / wrong"));

        Iterable<String> split = Splitter.on(",").
                trimResults().//去掉空格
                omitEmptyStrings().//去掉空字符串
                split("the ,quick, , brown         , fox,              jumps, over, the, lazy, little dog.");
        System.out.println(split);

        System.out.println(Splitter.on(";").limit(3).split("a;b;c;d"));

        /* 当需要去掉空的子字符串时，这个空的子字符串是不记入到limit中的， */
        System.out.println(Splitter.on(";").limit(3).omitEmptyStrings().split("a;b;;;;;c;d"));

        Map<String,String> split1 = Splitter.on(";").trimResults().withKeyValueSeparator("&").split("a&2;b&3");
        System.out.println(split1);

        Splitter splitter2 = Splitter.on('/').trimResults(CharMatcher.is('b')).omitEmptyStrings();
        Iterable<String> split2 = splitter2.split("wrong a/b/wrong /  a wrong");
        ArrayList<String> arrayList = Lists.newArrayList(split2);
        System.out.println(arrayList.size());
        for (String s : arrayList) {
            System.out.println(s);
        }
    }
}
